package cn.csc.module.order.mq.producer;

import cn.csc.framework.mq.core.bus.AbstractBusProducer;
import cn.csc.module.pay.message.PayOrderNotifyMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageConst;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 订单支付成功 通知库存扣减
 * topic: pay_order_notify_stock
 * @author liuchuang
 * @version 1.0
 * @date 2024/1/29 17:17
 * @modify
 */
@Slf4j
@Component
public class PayOrderNotifyStockProducer extends AbstractBusProducer {

    @Resource
    private StreamBridge streamBridge;


    /**
     * 发送 {@link PayOrderNotifyMessage} 消息
     *
     * @param merchantOrderId 订单id
     */
    public Boolean sendPayOrderNotifyStockMessage(String merchantOrderId) {
        PayOrderNotifyMessage lockMessage = new PayOrderNotifyMessage(merchantOrderId);

        Message<PayOrderNotifyMessage> message = MessageBuilder.withPayload(lockMessage)
                // 设置ORIGIN_MESSAGE_ID
                .setHeader(MessageConst.PROPERTY_ORIGIN_MESSAGE_ID, merchantOrderId)
                .build();
        // payOrderNotifyStock为事务消息topic。消息有两个状态，prepared和commited。
        // 当消息执行完send方法后，进入的prepared状态，进入prepared状态以后，就要执行executeLocalTransaction方法
        return streamBridge.send("payOrderNotifyStock-out-0", message);
    }

}
